﻿using Cms.Application.Admin.Views;
using Cms.Data;
using Cms.Domain;
using Cms.Domain.RepositoryInterfaces;

namespace Cms.Application.Admin.Presenters
{
    public class UploadCheckPresenter : BasePresenter
    {
        private readonly IUploadCheckView _view;

        public UploadCheckPresenter(IUploadCheckView view)
            : this(view, new RepositoryFactory())
        {
        }

        public UploadCheckPresenter(IUploadCheckView view, IRepositoryFactory repositoryFactory)
            : base(view, repositoryFactory)
        {
            _view = view;
        }

        public void CheckFiles(string selectedFiles)
        {
            Administrator administrator = AdministratorRepository.GetAdministrator(_view.AdministratorID);
            MediaFolder folder = MediaRepository.GetMediaFolder(administrator.SelectedMediaFolderID);
            string existingFiles = "";
            foreach (string selectedFile in selectedFiles.Split(",".ToCharArray()))
            {
                MediaFile file = folder != null
                                     ? folder.GetFile(selectedFile.Replace(" ", "-"))
                                     : administrator.CurrentSite.GetRootMediaFile(selectedFile.Replace(" ", "-"));
                if (file == null)
                    continue;
                if (existingFiles != "")
                    existingFiles += ", ";
                existingFiles += selectedFile;
            }
            _view.SetExistingFiles(existingFiles);
        }
    }
}